/**
 * Created by KaiseRR on 24.02.15.
 */
public class MessageCenter {

    private String messageToSend;
    private String receivedMessage;
    private boolean isDestroyed;
    private String clientIdentificator;

    public MessageCenter(String clientIdentificator) {
        this.clientIdentificator = clientIdentificator;
    }



    public synchronized String getMessageToSend() {
        while (this.messageToSend == null && this.receivedMessage != null && !this.receivedMessage.equals(messageToSend)) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
     //   if (messageToSend != null)
    //        System.out.println("Get message: " + messageToSend + " received message=" + receivedMessage);

        String messageToSend = this.messageToSend;
        clearMessageToSend();
        notify();
        return messageToSend;
    }

    public synchronized void setMessageToSend(String messageToSend) {
        while (this.messageToSend != null && receivedMessage == null && !this.messageToSend.equals(receivedMessage)) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    //   System.out.println("Set message: " + messageToSend);
        this.messageToSend = messageToSend;
        notify();
    }

    public synchronized void setReceivedMessage(String receivedMessage) {
        while (this.receivedMessage != null) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    //  System.out.println("Set received message: " + receivedMessage);
       this.receivedMessage = receivedMessage;
        notify();
    }
    public synchronized String getReceivedMessage() {
        while (receivedMessage == null) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        notify();
     //   System.out.println("Get received message:" + receivedMessage);
        return receivedMessage;
    }
    public synchronized void clearReceivedMessage() {
        if (receivedMessage != null) {
   //         System.out.println("Clear received message:" + receivedMessage);
            receivedMessage = null;
        }
        notify();
    }

    public synchronized void clearMessageToSend() {
        if (messageToSend != null) {
    //        System.out.println("Clear send message:" + messageToSend);
            this.messageToSend = null;
        }
        notify();
    }
    public void setDestroyed() {
        this.isDestroyed = true;
    }
    public boolean isDestroyed() {
        return isDestroyed;
    }

    public String getClientIdentificator() {
        return clientIdentificator;
    }
}
